package com.sskj.mine.asset;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.constraint.Guideline;
import android.text.TextUtils;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hjq.toast.ToastUtils;
import com.jakewharton.rxbinding2.widget.RxTextView;
import com.sskj.common.util.ClickUtil;
import com.sskj.lib.base.BaseActivity;
import com.sskj.lib.util.CoinUtil;
import com.sskj.mine.R;
import com.sskj.mine.R2;
import com.sskj.mine.asset.data.ExchangeConfigBean;

import java.math.BigDecimal;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @author Hey
 * Create at  2019/05/04
 */
public class ExchangeActivity extends BaseActivity<ExchangePresenter> {


    @BindView(R2.id.right_tv)
    TextView rightTv;
    @BindView(R2.id.team_tv)
    TextView teamTv;
    @BindView(R2.id.team_layout)
    LinearLayout teamLayout;
    @BindView(R2.id.team_total_tv)
    TextView teamTotalTv;
    @BindView(R2.id.reward_layout)
    LinearLayout rewardLayout;
    @BindView(R2.id.mineView)
    ImageView mineView;
    @BindView(R2.id.mineGuideline2)
    Guideline mineGuideline2;
    @BindView(R2.id.num_edt)
    EditText numEdt;
    @BindView(R2.id.exchange_unit_tv)
    TextView exchangeUnitTv;
    @BindView(R2.id.get_num_edt)
    EditText getNumEdt;
    @BindView(R2.id.get_unit)
    TextView getUnit;
    @BindView(R2.id.tvDesc)
    TextView tvDesc;
    @BindView(R2.id.tvFee)
    TextView tvFee;
    @BindView(R2.id.password_edt)
    EditText passwordEdt;
    @BindView(R2.id.submit_btn)
    Button submitBtn;
    ExchangeConfigBean data;
    boolean isDaiToUsdt = true;

    @Override
    public int getLayoutId() {
        return R.layout.mine_activity_exchange;
    }

    @Override
    public ExchangePresenter getPresenter() {
        return new ExchangePresenter();
    }

    @Override
    public void initView() {
        setTitle(getString(R.string.mine_exchangeActivity1));
        rightTv.setCompoundDrawablesWithIntrinsicBounds(R.mipmap.mine_icon_history, 0, 0, 0);
    }

    @Override
    public void initData() {
        mPresenter.getExchangeConfig(isDaiToUsdt ? "1" : "2");

    }

    @Override
    protected void initEvent() {
        ClickUtil.click(rightTv, () -> ExchangeRecordsActivity.start(this));
        ClickUtil.click(submitBtn, () -> {
            if (isEmpty(numEdt)) {
                ToastUtils.show(getString(R.string.mine_exchangeActivity2));
                return;
            }
            if (isEmpty(passwordEdt)) {
                ToastUtils.show(getString(R.string.mine_exchangeActivity3));
                return;
            }
            mPresenter.cash(isDaiToUsdt ? "1" : "2", numEdt.getText().toString(), passwordEdt.getText().toString());
        });
        ClickUtil.click(mineView, () -> {
            if (isDaiToUsdt) {
                isDaiToUsdt = false;
                teamTv.setText("USDT");
                exchangeUnitTv.setText("USDT");
                teamTotalTv.setText("GRCT");
                getUnit.setText("GRCT");
                mPresenter.getExchangeConfig(isDaiToUsdt ? "1" : "2");
            } else {
                isDaiToUsdt = true;
                teamTv.setText("GRCT");
                exchangeUnitTv.setText("GRCT");
                teamTotalTv.setText("USDT");
                getUnit.setText("USDT");
                mPresenter.getExchangeConfig(isDaiToUsdt ? "1" : "2");

            }
        });

    }

    public static void start(Context context) {
        Intent intent = new Intent(context, ExchangeActivity.class);
        context.startActivity(intent);
    }

    public void updateConfig(ExchangeConfigBean data) {
        this.data = data;
        tvDesc.setText(isDaiToUsdt ? String.format(getString(R.string.mine_exchangeActivity4), CoinUtil.keepCoin(data.getRate())) :
                String.format(getString(R.string.mine_exchangeActivity5), CoinUtil.keepCoin(data.getRate())));
        tvFee.setText(String.format(getString(R.string.mine_exchangeActivity6), "0", isDaiToUsdt ? "GRCT" : "USDT"));
        RxTextView.afterTextChangeEvents(numEdt)
                .subscribe(textViewAfterTextChangeEvent -> {
                    String text = textViewAfterTextChangeEvent.editable().toString();
                    if (TextUtils.isEmpty(text)) {
                        getNumEdt.setText("");
                        tvFee.setText(String.format(getString(R.string.mine_exchangeActivity6), "0", isDaiToUsdt ? "GRCT" : "USDT"));

                        return;
                    }
                    String s = new BigDecimal(text).multiply(new BigDecimal(data.getRate())).toPlainString();
                    getNumEdt.setText(CoinUtil.keepCoin(s));
                    tvFee.setText(String.format(getString(R.string.mine_exchangeActivity6), CoinUtil.keepCoin(new BigDecimal(text).multiply(new BigDecimal(data.getFee())).multiply(new BigDecimal("0.01")).toPlainString()), isDaiToUsdt ? "GRCT" : "USDT"));

                });
    }

    public void cashSuccess() {
        numEdt.setText("");
        passwordEdt.setText("");
        ExchangeRecordsActivity.start(this);
    }
}
